home *** CD-ROM | disk | FTP | other *** search
- Path: argonet.co.uk!argbe15
- From: Dave Mullard <dmullard@argonet.co.uk>
- Newsgroups: comp.lang.c++
- Subject: Re: How to get at base class fields from member functions?
- Date: Wed, 20 Mar 1996 02:50:32
- Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX
- Distribution: world
- Message-ID: <internews46B1A3DB3E@argonet.co.uk>
- References: <internews46B00D1FBD@argonet.co.uk> <4ihhkf$2ni@news5.erols.com> <internews46B0766104@argonet.co.uk> <marnoldDoGE1H.FMH@netcom.com> <marnoldDoGEGI.G09@netcom.com>
- Reply-To: Dave Mullard <dmullard@argonet.co.uk>
- NNTP-Posting-Host: an194.du.pipex.com
- X-Newsreader: VTi Voyager InterNews 0.15 for Acorn RISC OS (Patched by RA)
-
- Matt Arnold <marnold@netcom.com> wrote:
-
- >Dave Mullard <dmullard@argonet.co.uk> writes:
-
- >>Chris Cobb <ccobb@erols.com> wrote:
- >>> Dave Mullard <dmullard@argonet.co.uk> wrote:
- >>> >Given three classes A, B and C I want to have multiple As for each B
- >>> and
- >>> >multiple Bs for each C. To do this I could create the following.
- >>> >
- >>> >class b1 : public B
- >>> >{
- >>> >A a1;
- >>> >A a2;
- >>> >A a3;
- >>> >};
-
- >>> >class b2 : public B
- >>> >{
- >>> >A a1;
- >>> >A a2;
- >>> >A a3;
- >>> >A a4;
- >>> >A a5;
- >>> >};
-
- >>> >class c1 : public C
- >>> >{
- >>> >b1 b1;
- >>> >b2 b2;
- >>> >};
-
- >>> >The question is, how within functions for class B do I access fields
- >>> >within class C, and similarly, how within the class A functions do I
- >>> >access fields within class B?
-
-
- > snip
-
- >>I suppose at its simplest level the problem is this
-
- >>class B
- >>{
- >>int fred;
- >> class A
- >> {
- >> A() { };
- >> };
- >>A a1;
- >>A a2;
- >>};
-
- >>How within A::A do I refer to fred.
-
- >You don't.
-
- >Realize that, even though A is nested class of B, A and B are distinct
- >types and not "linked" in the way you seen to think they are. In your
- >sample code above, A has no more connection to or knowledge of B than
- >some other class C in a totally different part of your program.
-
- >If you need this kind of link, you must explicitly code it yourself.
- >For example, you could arrange to pass, to an instance of A, a
- >reference or pointer to an instance B, and access B::fred in *that*
- >instance.
-
- >A could have the following member...
-
- >void A::access_fred(B* b)
- > {
- > b.fred = 42;
- > }
-
- My problem is that I still get myself confused over the relationship
- between classes and that between objects. As you say, you need to pass
- pointers so I have settled for the following approach.
-
- C c1;
-
- B b1(&c1);
-
- A a11(&b1);
- A a12(&b1);
- A a13(&b1);
-
- B b2(&c1);
-
- A a21(&b2);
- A a22(&b2);
- A a23(&b2);
- A a24(&b2);
- A a25(&b2);
-
- Then each A can have a pointer to its B and the Bs to the C. It isnt text
- book c++ but its easy to program. It has the advantage that it works
- regardless of whether they are static, automatic or dynamic.
-
- BTW my compiler CFront does not fault
-
- b1 b1;
- B2 b2;
- --
- Dave Mullard <dmullard@argonet.co.uk>
-
-